<?php 
// require_once('/php/public/id.inc');挂系统跑就不要引用这个了。
error_reporting(E_STRICT);
date_default_timezone_set("Asia/Shanghai"); //设定时区东八区
$currentdatetime = date("Y/m/d H:i:s"); ##
include 'connect/mydb.class.php';
require_once("class.phpmailer.php");
require_once("class.smtp.php");
$oracle_db = new mydb("oracle");
// 删除15天前的所有资料。也就是每个月运行之前会删除前一个月资料。
// $del_command = "find /php/maildemo/uploads/excel -mtime +15 -type f -print0 | xargs -0 rm";
// system("$del_command");
$sql = "select mfg_no,part_no,get_osc_tran_user(source_code,purchase_no) as trans_user,
get_name(get_osc_tran_user(source_code,purchase_no) ) as trans_name,maker_code,maker_no,
order_no,current1,F_RETURN_OSC_MAKER_cname(maker_no) as maker_cname,part_name,draw_no,
piece,unit,unit_price,order_qty,in_factory_qty,check_ok_qty,check_bad_qty,in_stock_qty,total_amount,delivery_date,
back_date,create_date,tran_date,ifo_sure_date,osc_bu,code_name('osc_depart',osc_bu) as osc_bu_name,
bu_code,get_osc_empl_name(bu_code) as bu_code_name,buyer_code,get_osc_empl_name(buyer_code) as buyer_code_name
 from osc_ifo_order_view where delivery_date<sysdate-90 
and voucher_no is null";
$datas = $oracle_db->doSql($sql);
$titles = array("作番", "零件号", "计划人员", "姓名", "厂商代码", "厂商编号", "订单号", "交货地", "厂商名称", "部品名称",
    "图号", "片数", "单位", "单价", "订购量", "入厂量", "合格量", "不合格量", "入库量", "金额", "指定交期", "交期回复",
    "订购日期", "生管开单日", "确认交期", "采购部门", "部门名称", "采购人员码", "采购人员", "发票作业人员", "人员姓名"); //excel 列
$file_name = date("Y-m-d");
$data_cnt = count($datas, 0);
echo "data_cnt=" . $data_cnt;
if ($data_cnt > 0) {
    $attachment = sendExcel($file_name, $titles, $datas);
    $mail_subject = "超过3个月未完纳提醒";
    $mail_body = "各位主管，您好！!<br><br>交期超过3个月还未完纳资料如附件，请查收！<br>此邮件为系统自动发送，请勿回复!<br>商祺!<br><br>永大电梯设备(中国)有限公司<br>$currentdatetime";
    $mail = new PHPMailer(); //new一个PHPMailer对象出来
    $body = eregi_replace("[\]", '', $body); //对邮件内容进行必要的过滤
    $mail->CharSet = "UTF-8"; //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱
    $mail->IsSMTP(); // 设定使用SMTP服务
    $mail->SMTPDebug = 1;
    $mail->Host = "mail.yungtay.com.cn"; // SMTP 服务器
    $mail->Port = 25; // SMTP服务器的端口号
    $mail->Username = "notify"; // SMTP服务器用户名
    $mail->Password = "abc123123!!"; // SMTP服务器密码
    $mail->SetFrom('notify@yungtay.com.cn', '永大电梯邮件提醒');
    $mail->AddReplyTo("notify@yungtay.com.cn", "永大电梯邮件提醒");
    $mail->Subject = $mail_subject;
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->MsgHTML($mail_body);
    if (is_array($attachment)) { // 添加附件
        foreach ($attachment as $file) {
            is_file($file) && $mail->AddAttachment($file);
        } 
    } 
    $sql2 = "select employee_no,name,mail||mail_server as mail_address from employee where employee_no='69173' ";
    $mail_address = $oracle_db->doSql($sql2);
    foreach ($mail_address as $k => $val) {
        $address = $val['mail_address'];
        $address_name = iconv("GBK", "UTF-8", $val['name']);
        $mail->AddAddress($address, $address_name);
    } 
    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    } 
} 
function sendExcel($filename, $titles, $data) {
    header("Content-type: text/html; charset=UTF-8");
    if ($filename == '') {
        return "文件名称不能为空！";
    } 
    if ($titles == '') {
        return "标题一维数组不能为空！";
    } 
    if ($data == '') {
        return "内容二维数组不能为空！";
    } 
    foreach ($titles as $k2 => $v2) {
        $titles[$k2] = iconv("UTF-8", "GB2312", $v2);
    } 
    $title_implode = implode("\t", $titles);
    foreach ($data as $k => $v) {
        $data[$k] = implode("\t", $v);
    } 
    // 数据如果是GBK的应该是没有问题的。如果是utf-8执行下面的转换。
    // $data = eval('return '.iconv("UTF-8", "GB2312",var_export($data,true).';'));
    $data_implode = implode("\n", $data);
    $contents = $title_implode . "\n" . $data_implode;
    $file_path = "uploads/excel/";
    if (!is_dir("uploads/")) {
        mkdir("uploads/");
    } 
    if (!is_dir($file_path)) {
        mkdir($file_path);
    } 
    $file_name = $file_path . $filename . ".xls";
    $file_name = iconv("UTF-8", "GBK", $file_name);
    file_put_contents($file_name, $contents);
    $attachments = array($file_name);
    return $attachments;
} 

?>
